package com.addcn.android.design591.comm;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;

/**
 * Created by xuge on 2017/1/4.
 * 6.0权限动态申请
 */

public class PermissionUtils {
    public static int REQUEST_EXTERNAL_STORAGE = 1;

    public static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_PHONE_STATE
    };


    /**
     * @param mContext
     */
    public static void externalStorage(final Context mContext, PermissionCallBack mCallBack) {
        externalStorage(mContext, mCallBack, -1);
    }

    public static void externalStorage(final Context mContext, PermissionCallBack mCallBack, int requestCode) {
        int code = requestCode == -1 ? REQUEST_EXTERNAL_STORAGE : requestCode;
        //检查是否拥有相应的权限
        int permission = ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE);
        //没有权限发起申请
        if (permission != PackageManager.PERMISSION_GRANTED) {
            //6.0及以上版本才需要申请权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS_STORAGE, code);
            else {
                mCallBack.setOnPermissionListener(false);
            }
        } else {
            mCallBack.setOnPermissionListener(true);
        }
    }


    /**
     * 权限申请回调，需要在你调用的Activity中onRequestPermissionsResult调用该方法true申请权限成功false申请权限失败
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, PermissionCallBack mCallBack) {
        if (requestCode == REQUEST_EXTERNAL_STORAGE) {
            //判断权限是否申请成功
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                mCallBack.setOnPermissionListener(true);
            } else {
                mCallBack.setOnPermissionListener(false);
            }
            return;
        }

    }

    /**
     * 6.0以下申请打开权限管理
     *
     * @param mContext
     */

    public static void openPre(final Context mContext) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("當前無權限，將無法進行應用升級~,是否打开设置？");
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //跳转到设置页面，手动打开权限
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
                mContext.startActivity(intent);
            }
        });
        builder.setPositiveButton("取消", null);
        builder.show();
    }

    public interface PermissionCallBack {
        void setOnPermissionListener(Boolean bo);
    }

}

